package com.utils.wifi

import android.content.Context
import android.net.wifi.WifiManager
import android.os.Build
import java.lang.ref.Reference
import java.lang.ref.SoftReference

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2021/6/16
 */
object WifiUtils {

    var wifiStateCallBack: SoftReference<WifiStateCallBack>? = null

    private val wifi:IWifi by lazy {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            WifiImpl29()
        }else{
            WifiImpl()
        }
    }
    fun isWifiEnable(context: Context): Boolean {
        val wifiManager =
                context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        return wifiManager.isWifiEnabled
    }

    fun openWifi(context: Context){
        wifi.openWifi(context)
    }

    fun connectWifi(context: Context, ssid: String, pwd: String) {
        wifi.connectWifi(context, ssid, pwd)
    }

    fun registerWifiStateChangerReceiver(context: Context, receiver: WifiConnectReceiver, wifiStateCallBack: WifiStateCallBack) {
        this.wifiStateCallBack = SoftReference(wifiStateCallBack)
        wifi.registerWifiStateChangerReceiver(context, receiver, wifiStateCallBack)
    }

    fun unregisterWifiStateChangeReceiver(context: Context, receiver: WifiConnectReceiver?) {
        wifiStateCallBack?.clear()
        wifiStateCallBack = null
        receiver?.let {
            context.unregisterReceiver(it)
        }
    }

}